/**
 * Copyright &copy; 2012-2016 <a href="https://istep.lczyfz.com">Istep</a> All rights reserved.
 */
package com.lczyfz.istep.modules.resource.service;

import com.lczyfz.istep.common.persistence.Page;
import com.lczyfz.istep.common.service.CrudService;
import com.lczyfz.istep.modules.resource.dao.ResourceCoursewareCourseDao;
import com.lczyfz.istep.modules.resource.entity.ResourceCourse;
import com.lczyfz.istep.modules.resource.entity.ResourceCourseware;
import com.lczyfz.istep.modules.resource.entity.ResourceCoursewareCourse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 课件-课程中间表Service
 * @author dh
 * @version 2017-02-21
 */
@Service
@Transactional(readOnly = true)
public class ResourceCoursewareCourseService extends CrudService<ResourceCoursewareCourseDao, ResourceCoursewareCourse> {
	@Autowired
	private ResourceCoursewareService resourceCoursewareService;
	@Autowired
	private ResourceCourseService resourceCourseService;
	public ResourceCoursewareCourse get(String id) {
		return super.get(id);
	}
	
	public List<ResourceCoursewareCourse> findList(ResourceCoursewareCourse resourceCoursewareCourse) {
		return super.findList(resourceCoursewareCourse);
	}
	
	public Page<ResourceCoursewareCourse> findPage(Page<ResourceCoursewareCourse> page, ResourceCoursewareCourse resourceCoursewareCourse) {
		return super.findPage(page, resourceCoursewareCourse);
	}
	
	@Transactional(readOnly = false)
	public void save(String duallist,ResourceCourse resourceCourse) {
		String[] resourceCoursewareList = new String[0];
		if (duallist != null && !duallist.equals(""))
			resourceCoursewareList = duallist.split(",");
		logger.debug("length: [{}]", resourceCoursewareList.length);
		resourceCourse.setClassHour(resourceCoursewareList.length + "");


		int count=0;
		for (int i = 0; i < resourceCoursewareList.length; i++) {
			ResourceCourseware resourceCourseware=resourceCoursewareService.get(resourceCoursewareList[i]);
			if(resourceCourseware.getEnvironmentId()!=null&&!resourceCourseware.getEnvironmentId().equals("")) {
				logger.debug("EnvironmentId: [{}]", resourceCourseware.getEnvironmentId());
				count++;
			}
		}
		logger.debug("dingh===实验课时: [{}]", count);

		resourceCourse.setExperimentClassHour(count+"");
		resourceCourseService.save(resourceCourse);
		for (int i = 0; i < resourceCoursewareList.length; i++) {

			ResourceCoursewareCourse resourceCoursewareCourse=new ResourceCoursewareCourse();
			resourceCoursewareCourse.setCourseId(resourceCourse.getId());
			resourceCoursewareCourse.setCoursewareId(resourceCoursewareList[i]);
			super.save(resourceCoursewareCourse);
		}


	}
	@Transactional(readOnly = false)
	public void update(String duallist,ResourceCourse resourceCourse) {
		ResourceCoursewareCourse temp=new ResourceCoursewareCourse();
		temp.setCourseId(resourceCourse.getId());
		List<ResourceCoursewareCourse> resourceCoursewareCourseList=super.findList(temp);
		logger.debug("课程-课件中间表数量: [{}]", resourceCoursewareCourseList.size());

		String[] resourceCoursewareList = new String[0];
		if (duallist != null && !duallist.equals(""))
			resourceCoursewareList = duallist.split(",");
		logger.debug("length: [{}]", resourceCoursewareList.length);


		for (int i = 0; i < resourceCoursewareCourseList.size(); i++) {
			super.delete(resourceCoursewareCourseList.get(i));
		}
		resourceCourse.setClassHour(resourceCoursewareList.length + "");


		int count=0;
		for (int i = 0; i < resourceCoursewareList.length; i++) {
			ResourceCourseware resourceCourseware=resourceCoursewareService.get(resourceCoursewareList[i]);
			if(resourceCourseware.getEnvironmentId()!=null&&!resourceCourseware.getEnvironmentId().equals("")) {
				logger.debug("EnvironmentId1: [{}]", resourceCourseware.getEnvironmentId());
				count++;
			}
		}
		logger.debug("dingh===实验课时==1: [{}]", count);

		resourceCourse.setExperimentClassHour(count+"");
		resourceCourseService.save(resourceCourse);
		for (int i = 0; i < resourceCoursewareList.length; i++) {

			ResourceCoursewareCourse resourceCoursewareCourse=new ResourceCoursewareCourse();
			resourceCoursewareCourse.setCourseId(resourceCourse.getId());
			resourceCoursewareCourse.setCoursewareId(resourceCoursewareList[i]);
			super.save(resourceCoursewareCourse);
		}
	}
	@Transactional(readOnly = false)
	public void delete(ResourceCoursewareCourse resourceCoursewareCourse) {
		super.delete(resourceCoursewareCourse);
	}
	
}